<?php
/*******************************************************************************
 *
 *  Controller Name: 
 *
 *  Date: 
 *  
 *  Description: 
 *  
 *  Author: 
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-26   HAYDEN WAISANEN
 *  2012-04-26   HAYDEN WAISANEN
 *  2012-04-26   HAYDEN WAISANEN
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-23   HAYDEN WAISANEN
 *  2012-04-23   HAYDEN WAISANEN
 *  2012-04-23   HAYDEN WAISANEN
 *  2012-04-22   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-17   DAVID JARMAN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-16   HAYDEN WAISANEN
 * 
 *****************************************************************************/
?>
<?
class Analyst extends CI_Controller {

   /*****************************************************
   * Constructor
   *****************************************************/
   public function __construct()
   {
      parent::__construct(); //Manually call parent contructor
      $this->load->library('session');
      $this->load->helper(array('form','url'));
      $this->load->model('Analyst_model','analyst');
      //Check if user has analyst credentials TODO
      $current_user = $this->session->all_userdata();

      //Check to see if they have an entry in analyst
      if(array_key_exists('UserID',$current_user))
      {
         $result = $this->analyst->get($current_user["UserID"]);
         if( count( $result ) == 0)
            redirect('/user/login');
      }
      else 
         redirect('/user/login');
   }

   /*****************************************************
   * Index function
   *****************************************************/
   
   public function index()
   {
		
   }
   /*****************************************************
   * Assignirteam function
   *****************************************************/
   
   public function assignIncident()
   {
      $this->load->model('Incident_model','incident');

      $submission = $this->incident->get($this->input->post('IncidentID', TRUE));
      $submission = $submission[0];
      $submission['AssignedTeam'] = $this->input->post('irteam', TRUE);
      $submission['Status'] = 1;
      var_dump($submission);
      $this->incident->update($submission);
      redirect('analyst/viewincident/'.$this->input->post('IncidentID', TRUE));
		
   }

   public function markIrrelevant($ReportID)
   {
      $this->load->model('Report_model','report');

      $this->report->delete(array('ReportID'=> $ReportID));
      redirect('analyst/viewreports/');
		
   }



   /*****************************************************
   * Create Incident
   *****************************************************/
   public function createIncident()
   {
      //Load header with menu
      $this->load->helper('dashboard_helper');
      $data['menu'] = getMenu();
      $this->load->view('headerview',$data);



      $this->load->model('Incident_model','user');
      $this->load->library('form_validation');

      $this->form_validation->set_rules('Name', 'Name', 'required');
      $this->form_validation->set_rules('Description', 'Description', "required");
      if($this->form_validation->run() == FALSE)
      {
         $this->load->view('createincidentformview');
      }
      else
      {
         //Success
         $this->load->model('Incident_model','incident');
         $current_user = $this->session->all_userdata();

         $submission = array( 
            'Name' => $this->input->post('Name', TRUE),
            'AnalystDescription' => $this->input->post('Description', TRUE),
            'AnalystID' => $current_user['UserID'],
            'Status' => 0
            );
         $this->incident->insert($submission);

         //Set success message
         $this->session->set_flashdata('message','Incident successfully created');


         redirect('analyst/viewincidents/');
      }

      //Load the footer
      $this->load->view('footerview',$data);

   }

   /*****************************************************
   * View Reports
   *****************************************************/
   public function viewReports($how = '')
   {
      //Load header with menu
      $this->load->helper('dashboard_helper');
      $data['menu'] = getMenu();
      $this->load->view('headerview',$data);


      $this->load->model('Report_model','report');
      if( $how == 'awaitinganalysis' )
      {
         $results = $this->report->getall('WHERE IncidentID IS NULL ORDER BY ReportTime');
         $data["entries"] = $results;
         $data["title"]= "view Reports Awaiting Analysis";
      }
      else
      {
         $results = $this->report->getall('WHERE 1 ORDER BY ReportTime DESC');
         $data["entries"] = $results;
         $data["title"]= "View All Reports";
      }
      $format = array(
                        'ReportID'=> array("human_readable"=> "Report ID"),
                        'CustomerRepID' => array("human_readable"=> "CR ID"),
                        'City' => array("human_readable"=> "Location", "formatstring"=>"%s, %s", "formatparams"=> array('City','State')),
                        'ReportTime' => array("human_readable"=> "Report Time"),
                        'Status' => array("human_readable"=> "Status"),
                        'Viewedit' => array("human_readable"=> "View/Edit", "formatstring"=>"analyst/viewreport/%s", "formatparams"=> array('ReportID'), "link" => "View/Edit")
      
                     );
      $data["format"]= $format;

      $this->load->view('viewreportsview', $data); 

      //Load the footer
      $this->load->view('footerview',$data);

   }


   /*****************************************************
   * View Incidents
   *****************************************************/
   public function viewIncidents($how = '')
   {
      //Load header with menu
      $this->load->helper('dashboard_helper');
      $data['menu'] = getMenu();
      $this->load->view('headerview',$data);

      $this->load->library('form_validation');
      $current_user = $this->session->all_userdata();
      $this->load->model('Incident_model','incident');
      $this->load->model('IRTeam_model','irteam');



      if( $current_user["IsManager"] == 1 AND $how=='all' )
      {
         $results = $this->incident->getAll();
         $data["title"]= "View ALL Incidents";
         $data["entries"] = $results;
      }
      else if($how == 'irteam')
      {
         //Output Range getter
         $data["irteams"] = $this->irteam->getAll();
         $this->load->view('getdaterangeformview',$data);
         $this->form_validation->set_rules('start', 'Start Date', 'required');
         $this->form_validation->set_rules('end', 'End Date', "required");
         $this->form_validation->set_rules('irteam', 'IR Team', "required");
         if($this->form_validation->run() == FALSE)
         {
            $results = $this->incident->getToday(); //$this->input->post('IRTeamID', TRUE) ))/
            $data["title"]= "IR Team Current Incidents";
            $data["entries"] = $results;

         }
         else
         {
            //Success
            $start = $this->input->post('start', TRUE);
            $end = $this->input->post('end', TRUE);
            $results = $this->incident->getRange($start,$end);//$this->input->post('irteam', TRUE)); 
            $data["title"]= "View ALL Incidents FROM ".$start." to ".$end;
            $data["entries"] = $results;
         }
      }
      else if( $how == 'awaitingrepair' )
      {
         $results = $this->incident->get_where(array('Status' => 1));
         $data["entries"] = $results;
         $data["title"]= "Incidents Awaiting Repair";

      }
      else
      {
         $results = $this->incident->get_where(array('AnalystID' => $current_user['UserID']));
         $data["title"]= "View ".$current_user['UserID']."'s Incidents";
         $data["entries"] = $results;
      }
      $this->load->view('viewincidentsview',$data);

      //Load the footer
      $this->load->view('footerview',$data);
   }

   /*****************************************************
   * View Incident
   *****************************************************/
   public function viewIncident($IncidentID)
   {
      //Load header with menu
      $this->load->helper('dashboard_helper');
      $data['menu'] = getMenu();
      $this->load->view('headerview',$data);

      //Get User data
      $this->load->library('session');
      $this->load->model('IRTeam_model','irteam');
      $current_user = $this->session->all_userdata();

      
      $this->load->model('Incident_model','incident');
      $this->load->model('Report_model','report');
      $reports = $this->report->getWithCustomerInfo("WHERE IncidentID = $IncidentID");
      $results = $this->incident->get($IncidentID);
      $data["Incident"] = $results[0];
      $data['Reports'] = $reports;
      $data['user'] = $current_user;

      //Get all Reports to allow attaching
      $reports = $this->report->getAll();
      $data['Reportsall'] = $reports;
      $data['IncidentID'] = $IncidentID;
      $data["irteams"] = $this->irteam->getAll();

      $this->load->view('viewincidentview',$data);

      //Load the footer
      $this->load->view('footerview',$data);

   }
   /*****************************************************
   * View Report
   *****************************************************/
   public function viewReport($ReportID)
   {
      //Load header with menu
      $this->load->helper('dashboard_helper');
      $data['menu'] = getMenu();
      $this->load->view('headerview',$data);

       $this->load->model('Report_model','report');
       $reports = $this->report->getWithCustomerInfo("WHERE Report.ReportID = $ReportID");
       $data['Report'] = $reports[0];
      $this->load->view('viewreportview',$data);

      //Load the footer
      $this->load->view('footerview',$data);

   }
   /*****************************************************
   * Attach Reports to Incidents (it really toggles..)
   *****************************************************/
   public function attachIncident($ReportID, $IncidentID)
   {
      //Check to see if the report has an incident
      $this->load->helper(array('form','url'));
      $this->load->model('Report_model','report');
       $report = $this->report->get($ReportID);
      $report = $report[0];

      if( $report['IncidentID'] != NULL ) //Already Attached
      {
         $report['IncidentID'] = NULL;
         echo "Attach";
      }
      else //Attach it
      {
         $report['IncidentID'] = $IncidentID;
         echo "Detach";
      }
      $this->report->update($report);

   }


}
?>
